package com.fudan.framework.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fudan.framework.entity.RequestEntity;
import com.fudan.framework.entity.ResponseMsg;

/**
 * 批量删除的controller
 * @author Xuxinxing
 *
 * @param <T>
 */
public interface BatchDeleteController<T extends RequestEntity> extends FunctionalController<T>{
	    @RequestMapping("/deletes")
	    @ResponseBody
	    default public ResponseMsg doDeteleBatch(HttpServletRequest request) {
	        checkPermission("delete");
	        try {
	            Boolean result = deleteBatch(request);
	            if (result) {
	                return ResponseMsg.build(SUCCESS_CODE, SUCCESS);
	            } else {
	                return ResponseMsg.build(ADD_ERROR_CODE, ERROR);
	            }
	        } catch (Exception e) {
	            return ResponseMsg.build(SYSTEM_ERROR_CODE, e.getMessage());
	        }
	    }

	   default public Boolean deleteBatch(HttpServletRequest request) throws Exception {
	        String[] ids = request.getParameterValues("ids");
	        List<T> list = new ArrayList<>();
	        for (String id : ids) {
	            T t = injectService().get(id);
	            list.add(t);
	        }
	        Boolean result = injectService().deleteBatch(list);
	        return result;
	    }
}
